#!/bin/bash
#

##系统初始化脚本,1.关闭selinux,2.关闭防火墙,3.时间同步,4.主机名设置,5.主机名解析


## 关闭selinux

# 检查当前用户是否是root用户
if [ "$(id -u)" -ne 0 ]; then
   echo "This script must be run as root" 
   exit 1
fi

getenforce &> /dev/null
if [ $? -eq 0 ]; then
    echo "selinux就是关闭状态"
else 
    echo "selinux未关闭"
    # 备份当前的SELinux配置文件
    SELINUX_CONFIG="/etc/selinux/config"
    cp "$SELINUX_CONFIG" "${SELINUX_CONFIG}.bak.$(date +%Y%m%d%H%M%S)"

    # 修改SELinux配置文件，将SELINUX设置为disabled
    sed -i 's/^SELINUX=.*$/SELINUX=disabled/' "$SELINUX_CONFIG"

    # 临时关闭SELinux（需要重启才能完全生效）
    setenforce 0

    # 检查SELinux状态
    selinux_status=$(sestatus | grep -i "current mode")
    echo "SELinux status after temporary disable: $selinux_status"

    # 提醒用户重启系统以完全应用更改
    echo "Please restart your system for the changes to take full effect."

    exit 0
    
fi

## 关闭防火墙
systemctl status firewalld.service &> /dev/null
if [ $? -eq 0 ]; then
    echo "当前防火墙未关闭，准备关闭防火墙"
    systemctl stop firewalld &> /dev/null
    systemctl disable firewalld &> /dev/null
    systemctl status firewalld.service &> /dev/null
    if [ $? -ne 0 ]; then
	echo "当前防火墙已永久关闭"
    fi
else
    echo "当前防火墙就是关闭状态"
fi


